<?php
/**
 * 订单管理
 * Created by PhpStorm.
 * User: zhaofengjun
 * Date: 2019/3/28
 * Time: 15:16
 */

namespace app\admin\controller;
use app\common\model\Attachments;
use app\common\model\GoodsL;
use app\common\model\OrderDetails;
use app\common\model\Orders;
use app\common\model\Users;
use think\log;


class Order extends Base
{

    public function index()
    {
        $model = new Orders();
        $user = new Users();
        $pageParam = ['query' => []];
        if (isset($this->param['keywords']) && !empty($this->param['keywords'])) {
            $pageParam['query']['keywords'] = $this->param['keywords'];
            $ulist = $user->whereLike('name', "%" . $this->param['keywords'] . "%")->column('id');
            $this->assign('keywords', $this->param['keywords']);

            $model->where('user_id','in',$ulist);


        } else {
            $model->order('id', 'desc');
        }



        if($this->uid == 1){
            $list = $model
                ->with('gUser')
                ->paginate($this->webData['list_rows'], false, $pageParam);
        }else{
            $list = $model
                ->with('gUser')
                ->where('admin_uid',$this->uid)
                ->paginate($this->webData['list_rows'], false, $pageParam);
        }





        $this->assign([
            'list'      => $list,
            'total'     => $list->total(),
            'page'      => $list->render(),
        ]);
        return $this->fetch();
    }


    // 添加订单
    public function add()
    {

        if(isset($this->param['user_id']) && !empty($this->param['user_id'])){
            $this->param['admin_uid'] = $this->uid;
            $result = Orders::create($this->param);
            if ($result) {
                $redirect_uri = 'order_details?id='. $result['id'] ;
                return $this->success('添加成功', $redirect_uri);
            }

            return $this->error();
        }


        $model = new Users();
        $pageParam = ['query' => []];
        $list = $model
            ->where('admin_uid',$this->uid)->select();
            //->paginate($this->webData['list_rows'], false, $pageParam);
            //->paginate(15, false, $pageParam);


        $this->assign([
            'user' => $list
        ]);
        return $this->fetch();


    }


    // 给订单添加商品
    public function add_goods()
    {
        $model = new Orders();
        $model2 = new OrderDetails();
        if ($this->request->isPost()) {
            if(empty($this->param['price'])){
                $this->param['price'] = $this->param['advance_price'];
            }
            if(empty($this->param['goods_num'])){
                $this->param['goods_num'] = 1;
            }
            $this->param['subtotal'] = $this->param['price'] * $this->param['goods_num'];
            $result = $model2::create($this->param);
            if ($result) {
                // 更新订单总金额
                $money = $model2->where('order_id',$result['order_id'])
                    ->sum('subtotal');
                $result2 = $model::update([
                    'id' => $result['order_id'],
                    'total' =>$money
                ]);
                if (!$result2) {
                    return $this->error();
                }

                return $this->redirect('order_details', ['id' => $result['order_id']]);
            }
            return $this->error();
        }


        $this->assign([
            'goods_uid'      => $this->uid
        ]);
        return $this->fetch();

    }




    //订单详情
    public function order_details()
    {
        // 查看详情
        $order = Orders::get($this->id);
        $this->assign([
            'order' => $order,
            'user' =>Users::get($order['user_id'])
        ]);
        return $this->fetch();
    }

    // 删除整个订单
    public function del()
    {
        Log::info($this->param);
        $id     = $this->id;
        $result = Orders::destroy(function ($query) use ($id) {
            $query->whereIn('id', $id);
        });
        if ($result) {
            return $this->deleteSuccess();
        }
        return $this->error('删除失败');
    }

    // 删除订单中的某一项
    public function del_details()
    {
        $id     = $this->id;
        $result = OrderDetails::destroy(function ($query) use ($id) {
            $query->whereIn('id', $id);
        });
        if ($result) {
            return $this->deleteSuccess();
        }
        return $this->error('删除失败');
    }

    //支付/未支付
    public function disable()
    {
        $order         = Orders::get($this->id);
        $order->status = $order->status == 1 ? 0 : 1;
        $result       = $order->save();
        if ($result) {
            return $this->success();
        }
        return $this->error();
    }


}